<template>
	
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
		<!-- <button type="primary" class="btn-login" @click="getUserInfo">一键登录</button> -->
		<button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
		<view class="tips-text">登录后尽享更多权益</view>
	</view>
</template>
<script>
	import { mapMutations} from 'vuex'
	export default{

		data(){
			return{
				
			}
		},
		methods:{
			...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
			 getUserInfo(e) {
			    // 判断是否获取用户信息成功
			    if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权！')
			
			    // 获取用户信息成功， e.detail.userInfo 就是用户的基本信息
			    console.log(e.detail.userInfo)
				this.updateUserInfo(e.detail.userInfo)
				 // 获取登录成功后的 Token 字符串
				  this.getToken(e.detail)
			  },
			  // 调用登录接口，换取永久的 token
			 async getToken(info){
				 const [err,res] = await uni.login().catch(err=>err)
				 console.log(res)
				 if(err||res.errMsg!=='login:ok') return uni.$showMsg('登录失败')
				 const query = {
					 code:res.code,
					 encrypteData:info.encrypteData,
					 iv:info.iv,
					 rawData:info.rawData,
					 signature:info.signature
				 }
				 
				 const {data:loginResult}=await uni.$http.post('/api/public/v1/users/wxlogin',query)
				 console.log(loginResult)
				 if(loginResult.meta.status!==400){
					// this.token='Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo',
					return uni.$showMsg('登录失败！')
				 }
				 uni.$showMsg('登录成功！')
				 this.updateToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
			 }
		}
	}
</script>
<style lang="scss">
	.login-container{
		height: 750px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f8f8f8;
		.btn-login{
			width: 90%;
			border-radius: 100px;
			margin: 15px 0;
			background-color: #c00000;
		}
		.tips-text{
			font-size: 12px;
			color: gray;
		}
	}
</style>
